#include "BSearch.h"
#include <string.h>

int cmp(const void* left, const void* right)
{
    return *((int*)left) - *((int*)right);
}

int BSearch::SetUpData(int* pData, int iLen)
{
    if (m_pData)
    {
        delete m_pData;
    }
    m_iLen = iLen;

    m_pData = new int[iLen];
    if (NULL == m_pData)
    {
        return -1;
    }

    m_iLen = iLen;
    memcpy(m_pData, pData, sizeof(pData[0])*iLen);

    qsort(m_pData, m_iLen, sizeof(m_pData[0]), cmp);

    return 0;
}

int BSearch::Test(int iKey)
{
    bsearch(&iKey, m_pData, m_iLen, sizeof(m_pData[0]), cmp);

    return iKey;
}

